package com.zhhtech.zhh.ui.dialog

import android.Manifest
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.view.LayoutInflater
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.lxj.xpopup.core.BottomPopupView
import com.zhhtech.zhh.R
import com.zhhtech.zhh.databinding.DialogWeComBinding
import com.zhhtech.zhh.ui.ext.permissionRequest
import com.zhhtech.zhh.ui.ext.toastMessageLong
import com.zhhtech.zhh.ui.utils.AlbumUtils
import com.zhhtech.zhh.ui.utils.GlideUtils

class WeComDialog(
    context: Context,
    private val weComImg: MutableList<String>,
    private val phone: String,
) : BottomPopupView(context) {
    private lateinit var vb: DialogWeComBinding
    override fun addInnerContent() {
        vb = DialogWeComBinding.inflate(LayoutInflater.from(context), bottomPopupContainer, false)
        bottomPopupContainer.addView(vb.root)
    }

    override fun onCreate() {
        super.onCreate()
        vb.ivClose.setOnClickListener { dismiss() }
        vb.textSys.text = "保存图片到相册，微信\"扫一扫\"打开\n添加客服企业微信，享受一对一服务"
        var weComImgUrl = ""
        if (weComImg.isEmpty()) {
            vb.ivWeComKf.setImageResource(R.mipmap.we_com_kf_1)
        } else {
            weComImgUrl = weComImg[(0 until weComImg.size).random()]
            GlideUtils.intoView(context, weComImgUrl, vb.ivWeComKf)
        }
        vb.buttonCallKf.setOnClickListener {
            context.startActivity(Intent(Intent.ACTION_DIAL, Uri.parse("tel:${phone}")))
        }
        vb.buttonSave.setOnClickListener {
            val saveBitmap = {
                if (weComImg.isEmpty()) {
                    AlbumUtils.saveBitmap(
                        context, BitmapFactory.decodeResource(context.resources, R.mipmap.we_com_kf_1),
                        "debtOptim_${System.currentTimeMillis()}.png"
                    )
                } else {
                    Glide.with(context)
                        .asBitmap()
                        .load(weComImgUrl)
                        .into(object : CustomTarget<Bitmap>() {
                            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                                AlbumUtils.saveBitmap(
                                    context, resource,
                                    "debtOptim_${System.currentTimeMillis()}.png"
                                )
                            }

                            override fun onLoadCleared(placeholder: Drawable?) {
                            }
                        })
                }
                "保存成功".toastMessageLong()
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) {
                saveBitmap()
            } else {
                context.permissionRequest {
                    permission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    onGranted {
                        saveBitmap()
                    }
                }
            }
        }
    }
}